import {Base}  from '../../utils/util.js';


class Order extends Base{
   constructor(){
     super();
     this._storageKeyName = 'newOrder';
   }

  /**下订单 */
  doOrder(param,callBack){
    var that = this;
    that.request({
      url:'order',
      method:'POST',
      data:{
        products:param
      },
      success:function(res){
        that.execSetStorageSync(true);
        callBack && callBack(res);
      },
      fail:function(err){}
    })
  }

  /**支付 */
  execPay(orderid,callBack){
    this.request({
      url:'pay/pre_order',
      method:'POST',
      data: { id: orderid},
      success:function(res){
        console.log('execPay:',res);
        var timeStamp = res.timeStamp;
        if(timeStamp){
          wx.requestPayment({
            'timeStamp': timeStamp.toString(),
            'nonceStr': res.noceStr,
            'package': res.package,
            'signType': res.signType,
            'paySign': res.paySign,
             success: function (res) {
               callBack && callBack(2)//支付成功
             },
             fail: function (res) {
               callBack && callBack(1)//支付失败·
             }
          })
        }else{
          callBack &&callBack(0);
        }
        
      }
    })
  }


  /**获取订单列表 */
  getOrderList(pindex,callBack){
    this.request({
      url:'order/by_user',
      data:{
        page:pindex,
      },
      success:function(res){
        callBack && callBack(res);
      }
    })
  }


  /**是否有新的订单 */



  /**本地缓存的更新/保存 */
  execSetStorageSync(data){
    wx.setStorageSync(this._storageKeyName, data);
  }

  



}



export { Order };

